package com.gxa.controller;

import com.gxa.BusinessException;
import com.gxa.pojo.GoodsStorage;
import com.gxa.service.StorageManageService;
import com.gxa.utils.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 陈静
 * @date 2020/9/27 16:55
 */
@Controller
@RequestMapping("/storageManage")
@CrossOrigin
public class StorageManageController {
    @Resource
    StorageManageService storageManageService;

    /**
     *  查询所有品牌名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findBrandName",method = RequestMethod.POST)
    public Map<String,Object> findBrandName() throws BusinessException {
        List<Map<String, Object>> brandName = storageManageService.findBrandName();
        JsonResult jsonResult = new JsonResult();
        if(brandName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(brandName);
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     *  查询所有分类名称
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findLevelName",method = RequestMethod.POST)
    public Map<String,Object> findLevelName() throws BusinessException {
        List levelName = storageManageService.findLevelName();
        JsonResult jsonResult = new JsonResult();
        if(levelName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(levelName);
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     * 根据时间或者供货厂商或者进货仓库或者审核状态查询
     * @param map
     * @return 入库信息
     */
    @ResponseBody
    @RequestMapping(value = "/findStorageManage",method = RequestMethod.POST)
    public Map<String,Object> findStorageManage(@RequestBody(required = false) Map<String,Object> map)  {
        JsonResult jsonResult = new JsonResult();
        String  number= map.get("number").toString();
        String size= map.get("size").toString();
        Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
        Integer limit=Integer.parseInt(size);
        map.put("offset",offset);
        map.put("limit",limit);
            List<Map<String, Object>> storageManage = storageManageService.findStorageManage(map);
           Integer integer = storageManageService.countFindStorageManage(map);

            jsonResult.setData(storageManage);
            jsonResult.put("count",integer);
            jsonResult.setCode("200");
            jsonResult.setMsg("success");

        return jsonResult.getValues();
    }

    /**
     *  查询对应的入库信息
     * @param map id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findStorageManageById",method = RequestMethod.POST)
    public Map findStorageManageById(@RequestBody Map map) throws BusinessException {
        List<Map<String, Object>> storageManage = storageManageService.findStorageManageById(Integer.parseInt(map.get("id").toString()));
        JsonResult jsonResult = new JsonResult();
        if(storageManage!=null) {
            jsonResult.setCode("200");
            jsonResult.put("storageManage", storageManage);
            jsonResult.setMsg("success");
        }else {
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     *  删除对应id的库存信息
     * @param map
     * @return
     */
    @ResponseBody
@RequestMapping(value = "/deleteStorageManageById",method = RequestMethod.POST)
    public Map deleteStorageManageById(@RequestBody Map map) throws BusinessException {
        Integer line = storageManageService.deleteStorageManageById(Integer.parseInt(map.get("id").toString()));
        JsonResult jsonResult = new JsonResult();
        if(line>-1) {
            jsonResult.setMsg("success");
            jsonResult.setCode("200");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    // number 页码    size：每页显示记录数

    /**
     *
     * @param map 起始页 每一页大小
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findAllStorageManageByPage",method = RequestMethod.POST)
    public Map findAllStorageManageByPage(@RequestBody Map map){
        String  number= map.get("number").toString();
        String size= map.get("size").toString();
        Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
        Integer limit=Integer.parseInt(size);
        List<Map<String, Object>> allStorageManage = storageManageService.findAllStorageManage(offset, limit);
       Integer count = storageManageService.countFindAllStorageManage(); // select count(*) from
        JsonResult jsonResult = new JsonResult();
       jsonResult.setData(allStorageManage);
       jsonResult.put("count",count);
       return jsonResult.getValues();
    }

    /**
     * 修改库存管理信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStorageManage",method = RequestMethod.POST)
    public Map updateStorageManage(@RequestBody Map map) throws BusinessException {
        Integer id = Integer.parseInt(map.get("id").toString());
        map.put("id",id);
        Integer integer = storageManageService.updateStorageManage(map);
        JsonResult jsonResult = new JsonResult();
        if(integer>-1){
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        } else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }



}
